package javax.servlet

import com.wangwei.modules.utils.HttpResponseUtil
import com.wangwei.modules.utils.HttpResponseUtil.ContentType
import com.wangwei.modules.utils.StreamUtil
import org.springframework.ui.Model
import java.io.BufferedOutputStream
import java.util.Enumeration
import com.wangwei.modules.utils.JspUtil
import com.wangwei.modules.utils.TextUtil
import com.wangwei.modules.container.WebContainer

interface ServletContext {
	fun getContextPath(): String
	fun getContext(uripath: String): ServletContext
	fun getMajorVersion(): Int
	fun getMinorVersion(): Int
	fun getEffectiveMajorVersion(): Int
}

interface ServletConfig {
	fun getServletName(): String
	fun getServletContext(): ServletContext
	fun getInitParameter(name: String): String
	fun getInitParameterNames(): Enumeration<String>
}

interface ServletRequest {


	fun getParameter(name: String): String?


	fun getParameterMap(): HashMap<String, Array<String>>


}

interface ServletResponse {
	fun getOutputStream(): BufferedOutputStream

	/**
	 *跳转页面
	 */
	fun location(path: String, model: Model?) {

		var jspPath = WebContainer.WEB_ROOT_PATH + path;
		if (model == null) {
			var jspStr = TextUtil.readToString(jspPath)
			output(jspStr, ContentType.html)
		} else {
			var jspStr = TextUtil.readToString(jspPath)

			var newJsp = JspUtil.translateJSP(jspStr, model.asMap())

			output(newJsp, ContentType.html)
		}
	}


	fun output(str: String, contentType: ContentType) {
		output(StreamUtil.String2Byte(str), contentType)
	}

	fun output(buff2: ByteArray, contentType: ContentType) {
		try {
			// 获取输出流
			var printWriter = getOutputStream()
			var a = HttpResponseUtil.addHttp1OKHeader(buff2, contentType)
			printWriter.write(a, 0, a.size)
			printWriter.close()
		} catch (e: Exception) {

		}
	}

}

interface Servlet {

	fun _init(config: ServletConfig?)

	fun getServletConfig(): ServletConfig?

	fun service(request: ServletRequest, responce: ServletResponse)


	fun getServletInfo(): String?


	fun destroy()

}
